package com.jader.adapter;

import java.util.ArrayList;
import java.util.List;

import com.jader.beans.LunTan_ALLHuiFu;
import com.zlp.jader.R;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ReplyAdapter extends BaseAdapter {

	private List<LunTan_ALLHuiFu> parent_list, all_list;
	private Context context;
	private LayoutInflater inflater;
	private String username;

	public ReplyAdapter(Context context, List<LunTan_ALLHuiFu> parent_list,
			List<LunTan_ALLHuiFu> all_list, String username, Handler handler) {
		this.parent_list = parent_list;
		Log.e("list", parent_list.toString());
		this.all_list = all_list;
		this.context = context;
		this.handler = handler;
		this.username = username;
		inflater = LayoutInflater.from(this.context);

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return parent_list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return parent_list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	private ViewHolder viewHolder;
	private ReplyAdapter adapter;
	private ArrayList<LunTan_ALLHuiFu> list;
	private Handler handler;

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		if (arg1 == null) {
			arg1 = inflater.inflate(R.layout.replay_adapter, null);
			viewHolder = new ViewHolder();
			viewHolder.content = (TextView) arg1
					.findViewById(R.id.reply_content);
			viewHolder.username = (TextView) arg1
					.findViewById(R.id.reply_username);
			viewHolder.listView = (ListView) arg1
					.findViewById(R.id.reply_reply);
			viewHolder.bei_reply_username = (TextView) arg1
					.findViewById(R.id.bei_reply_username);
			arg1.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) arg1.getTag();
		}

		LunTan_ALLHuiFu lun_tan = parent_list.get(arg0);
		viewHolder.content.setText(lun_tan.getContent());
		viewHolder.username.setText(lun_tan.getUser().getUsername());
		viewHolder.bei_reply_username.setText(username + "：");
		list = new ArrayList<LunTan_ALLHuiFu>();
		for (LunTan_ALLHuiFu lun_tan2 : all_list) {

			if (lun_tan2.getHuifu_id() == lun_tan.getTiezi_id()) {
				list.add(lun_tan2);
			}
		}
		adapter = new ReplyAdapter(context, list, all_list, lun_tan.getUser()
				.getUsername(), handler);
		viewHolder.listView.setAdapter(adapter);
		viewHolder.bei_reply_username
				.setOnClickListener(new TextviewClickListener(arg0));
		viewHolder.username.setOnClickListener(new TextviewClickListener(arg0));
		viewHolder.content.setOnClickListener(new TextviewClickListener(arg0));
		return arg1;
	}

	class ViewHolder {
		TextView username, content, bei_reply_username;
		ListView listView;
	}

	private final class TextviewClickListener implements OnClickListener {
		private int position;

		public TextviewClickListener(int position) {
			this.position = position;
		}

		@Override
		public void onClick(View arg0) {
			Message message = new Message();
			message.what = 10;
			message.obj = parent_list.get(position);
			handler.sendMessage(message);

		}

	}

	@Override
	public void notifyDataSetChanged() {
		if (adapter != null) {
			adapter.notifyDataSetChanged();
		}
		super.notifyDataSetChanged();
	}

}
